package com.solcoo.common.security;

import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class EncryptUtil {
	private static final String TAG = "EncryptUtil";

	public static String md5Hex(String input) {
		return md5Hex(input.getBytes());
	}

	public static String md5Hex(byte[] data) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(data);
			BigInteger number = new BigInteger(1, messageDigest);
			String md5 = number.toString(16);

			while (md5.length() < 32)
				md5 = "0" + md5;
			return md5;
		} catch (NoSuchAlgorithmException e) {
			Log.e(TAG, "md5Hex is fail", e);
			return new String(data);
		}
	}

	public static String md5HexForAutoLogin(String input) {
		try {
			return DigestUtils.md5ToHex(new ByteArrayInputStream(input
					.getBytes("GBK")));
		} catch (Exception e) {
			Log.e(TAG, "md5HexForAutoLogin is fail", e);
			return input;
		}
	}
}
